package cn.tedu.datamanager.controller;


import cn.tedu.datamanager.mapper.ProductMapper;
import cn.tedu.datamanager.pojo.entity.Product;
import cn.tedu.datamanager.pojo.vo.ServicesListItemVO;
import cn.tedu.datamanager.serve.IproductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/product")
@Api(tags = "05. 商品管理模块")
public class ProductController {
    @Autowired
    ProductMapper mapper;
    @Autowired
    IproductService service;

    @PostMapping("/insert")
    @ApiOperation("新增商品")

    public void insert(@RequestBody Product product) {
        System.out.println("product = " + product);
        mapper.insert(product);
    }

    @GetMapping("select-server-id")
    @ApiOperation("查询商品类别列表")
    public List<Integer> selectServerId(){
       return service.selectServerId();
    }

    @GetMapping("/select")
    @ApiOperation("查询商品列表")
    public List<Product> select() {
        System.out.println("进select方法了");
        System.out.println("mapper.select():"+mapper.select());
        for (Product product:mapper.select()) {
            System.out.println("进for循环了");
            Long result=(product.getDay().getTime() - new Date().getTime())/ (24 * 60 * 60 * 1000);
            System.out.println(product.getId()+":"+product.getDay().getTime());
            System.out.println("new Date().getTime():"+new Date().getTime());
            System.out.println(product.getId()+"计算结果为:"+result);
            if ((product.getDay().getTime() - new Date().getTime())/ (24 * 60 * 60 * 1000) < 0) {
                mapper.updateOverdue1(product.getId());
            } else if ((product.getDay().getTime() - new Date().getTime())/ (24 * 60 * 60 * 1000) <1) {
                mapper.updateOverdue2(product.getId());
            } else if ((product.getDay().getTime() - new Date().getTime())/ (24 * 60 * 60 * 1000) <= 2) {
                mapper.updateOverdue3(product.getId());
            }else{
                mapper.updateOverdue4(product.getId());
            }
            System.out.println("走for循环了");
        }
        List<Product> list = mapper.select();
        System.out.println("返回的商品列表具体数据是:"+list);
        return list;
    }

    @GetMapping("/selectNotOverDue")
    @ApiOperation("查询未过期商品列表")
    public List<Product> selectNotOveDue(){
        List<Product> products = mapper.selectNotOverDue();
        return products;
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("根据id删除商品")
    public void delete(@PathVariable int id) {
        System.out.println("要删除的商品id是:"+id);
        String url = mapper.selectUrlById(id);
        new File("D:/Tedu/tedu/data-management-system/files" + url).delete();
        mapper.deleteById(id);
    }


    @GetMapping("/selectIndex")
    @ApiOperation("查询标准商品列表")
    public List<Product> selectIndex() {
        return mapper.selectIndex();
    }


    @GetMapping("/selectById")
    @ApiOperation("根据商品id查询商品信息")
    public Product selectById(int id) {
        System.out.println("id = " + id);
        return mapper.selectById(id);
    }

    @PostMapping("/update")
    @ApiOperation("修改商品信息")
    public void update(@RequestBody Product product, String oldUrl) {
        System.out.println("product = " + product);
        mapper.update(product);
        delectOldUrl(oldUrl);
    }

    @PostMapping("/deleteOldUrl/{oldUrl}")
    @ApiIgnore
    public void delectOldUrl(@PathVariable String oldUrl) {
        new File("D:/Tedu/tedu/data-management-system/files" + oldUrl).delete();
        System.out.println("原商品图片已从文件夹删除");
    }

}
